package com.lo.controller;

import com.lo.common.JsonResult;
import com.lo.entity.Message;
import com.lo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("message")
public class MessageController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("getUser2TeacherMessage/{id}")
    public JsonResult getUser2TeacherMessage(@PathVariable Long id) {
        ListOperations<String, Object> opsForList = redisTemplate.opsForList();
        List<Object> messages = opsForList.range("user:" + id, 0, -1);
//        System.out.println(messages.get(0));
        if (!messages.isEmpty()){
            opsForList.rightPop("user:"+id);
            Message message = (Message) messages.get(0);
            return new JsonResult(0,message.getContent(),message.getState());
        }
        return new JsonResult(0,"消息列表为空",null);
    }

    @RequestMapping("getUserBannedMessage/{id}")
   public JsonResult getUserBannedMessage(@PathVariable Long id){
       ListOperations<String, Object> opsForList = redisTemplate.opsForList();
       //3代表被封禁
       List<Object> messages = opsForList.range("user:banned:" + id, 0, -1);
//       System.out.println(messages.get(0));
       if (!messages.isEmpty()){
           opsForList.rightPop("user:banned:"+id);
           Message message = (Message) messages.get(0);
           return new JsonResult(0,message.getContent(),message.getState());
       }
       return new JsonResult(0,"消息列表为空",null);
   }
//   @RequestMapping("getTeacherBannedMessage/{id}")
//   public JsonResult getTeacherBannedMessage(@PathVariable Long id){
//       ListOperations<String, Object> opsForList = redisTemplate.opsForList();
//       //3代表被封禁
//       List<Object> messages = opsForList.range("teacher:banned:" + id, 0, -1);
////       System.out.println(messages.get(0));
//       if (!messages.isEmpty()){
//           opsForList.rightPop("teacher:banned:"+id);
//           Message message = (Message) messages.get(0);
//           return new JsonResult(0,message.getContent(),message.getStatusCode());
//       }
//       return new JsonResult(0,"消息列表为空",null);
//   }
}
